package com.hec.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hec.yygh.common.result.R;
import com.hec.yygh.hosp.repository.DepartmentRepository;
import com.hec.yygh.hosp.service.DepartmentService;
import com.hec.yygh.model.hosp.Department;
import com.hec.yygh.vo.hosp.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    //上传科室信息
    @Override
    public void saveDepartmentList(Map<String, Object> stringObjectMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(stringObjectMap), Department.class);
        //查询mongodb是否有科室信息
        Department departmentInfo = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentInfo != null){
            //有  更新操作
            department.setCreateTime(departmentInfo.getCreateTime());
            department.setUpdateTime(departmentInfo.getUpdateTime());
            department.setIsDeleted(departmentInfo.getIsDeleted());
            department.setId(departmentInfo.getId());
            departmentRepository.save(department);
        }else{
            //没有  做添加操作
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page findDepartmentPage(Map<String, Object> stringObjectMap) {
        String hoscode = (String)stringObjectMap.get("hoscode");
        Integer page = Integer.parseInt((String) stringObjectMap.get("page"));
        Integer limit = Integer.parseInt((String) stringObjectMap.get("limit"));
        Department department = new Department();
        department.setHoscode(hoscode);
        log.info("department================="+department);
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");
        Example example = Example.of(department);
        PageRequest pageRequest = org.springframework.data.domain.PageRequest.of(page,limit,sort);
        Page all = departmentRepository.findAll(example, pageRequest);
        log.info("page============>"+page);
        log.info("limit==================>"+limit);
        log.info("all1=====================>"+all);
        return all;

    }

    @Override
    public void removeDepartmentByDepcode(Map<String, Object> stringObjectMap) {
        String depcode = (String)stringObjectMap.get("depcode");
        String hoscode = (String)stringObjectMap.get("hoscode");
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null){
            departmentRepository.deleteById(department.getId());

        }
    }

    @Override
    public R getAllDepartments(String hoscode) {
        //根据医院编号查询所有的科室信息
        List<Department> departments = departmentRepository.getDepartmentByHoscode(hoscode);
        //组装前端所需要的数据  key:大科室编号   value：当前大科室底下的所有子科室列表
        Map<String, List<Department>> collect = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));
        List<DepartmentVo> bigDeptList = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            DepartmentVo departmentVo = new DepartmentVo();
            //设置大科室编号
            String key = entry.getKey();
            departmentVo.setDepcode(key);
            List<Department> value = entry.getValue();
            //设置大科室名称
            String bigname = value.get(0).getBigname();
            departmentVo.setDepname(bigname);
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : value) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                String depcode = department.getDepcode();
                String depname = department.getDepname();
                departmentVo1.setDepcode(depcode);
                departmentVo1.setDepname(depname);
                children.add(departmentVo1);
            }
            departmentVo.setChildren(children);
            bigDeptList.add(departmentVo);
        }
        return R.ok().data("list",bigDeptList);
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }

    @Override
    public Department getDepartmentByDepcode(String hoscode,String depcode) {

        Department department = departmentRepository.getDepartmentByDepcodeAndHoscode(depcode,hoscode);
        return department;

    }


}